<?php
//+---------------------------------------------------------------------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+---------------------------------------------------------------------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+---------------------------------------------------------------------------------------------------------------------
//| 
//+---------------------------------------------------------------------------------------------------------------------


namespace app\common\plugins\flow;


use app\admin\model\Config;
use app\admin\model\Menu;
use app\common\exception\PluginException;
use app\common\plugins\Plugin;
use helper\file\File;
use think\Exception;

class UnInstall
{
    protected $plugin;

    protected $pluginPath = '';

    /**
     * 插件名
     * @var string
     */
    protected $name = '';

    /**
     * 插件标识
     * @var string
     */
    protected $flag = '';

    protected $model = null;

    public function __construct ( Plugin $plugin )
    {
        $this->plugin     = $plugin;
        $this->flag       = $this->plugin->getFlag();
        $this->pluginPath = $this->plugin->getPluginPath() . $this->flag . DIRECTORY_SEPARATOR;
        $this->name       = $this->plugin->getName();
        $this->model      = new \app\admin\model\Plugin();
    }

    public function make ()
    {
        if ( !$this->plugin->getInstalled() ) {
            throw new PluginException(sprintf('插件 %s 还没有安装' , $this->flag));
        }

        $this->model->startTrans();
        try {
            $this->unInstallInfo();
            $this->unInstallConfig();
            $this->unInstallMenus();
            $this->unInstallStaticResource();
            $this->model->commit();
            return true;
        } catch ( \Exception $e ) {
            $this->model->rollback();
            throw new PluginException($e->getMessage());
        }
    }

    protected function unInstallInfo ()
    {
        if ( $this->model->failException(true)->where('flag' , $this->flag)->update([ 'is_install' => 0 ]) ) {
            return true;
        }
        throw new \Exception('删除插件基本信息失败');
    }

    protected function unInstallConfig ()
    {
        $model = new Config();
        if ( $model->failException(true)->where('group' , $this->flag)->delete() !== false ) {
            return true;
        }

        throw new \Exception('删除插件配置信息失败');
    }

    protected function unInstallMenus ()
    {
        $res = Menu::destroy(function ( $query ) {
            $query->where('flag' , $this->flag);
        },true);

	    if ( false !== $res ) {
            return true;
        }
	    throw new \Exception('删除插件菜单失败');
    }

    protected function unInstallStaticResource ()
    {
//        File::remove(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $this->flag);
        File::remove(app()->getRootPath() . 'plugins' . DIRECTORY_SEPARATOR . $this->flag . DIRECTORY_SEPARATOR . 'install.lock');
        return true;
    }
}